<template xmlns:width="http://www.w3.org/1999/xhtml">
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="订单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="orderNo">
        <el-select
            v-model="queryParams.orderStatus"
            placeholder="订单状态"
            style="width: 140px"
            @blur="handleQuery"
        >
          <el-option label="待支付" value="0"></el-option>
          <el-option label="待发货" value="1"></el-option>
          <el-option label="已发货" value="2"></el-option>
          <el-option label="完成" value="3"></el-option>
          <el-option label="取消" value="-1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="cancelTime">
        <el-date-picker
            v-model="betweenTime"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @blur="chooseDate"
        />
      </el-form-item>
      <el-form-item label="收货人姓名" prop="receiverName" label-width="100px">
        <el-input
          v-model="queryParams.receiverName"
          placeholder="请输入收货人姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收货人电话" prop="receiverPhone" label-width="100px">
        <el-input
          v-model="queryParams.receiverPhone"
          placeholder="请输入收货人电话"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['order:orderInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderInfoList" @selection-change="handleSelectionChange">
      <el-table-column label="订单号" align="center" prop="orderNo" width="300px"/>
      <el-table-column label="订单总额" align="center" prop="totalAmount" width="80px"/>
      <el-table-column label="订单状态" align="center" prop="orderStatus" width="80px" #default="scope">
        <span v-if="scope.row.orderStatus==0">待支付</span>
        <span v-if="scope.row.orderStatus==1">待发货</span>
        <span v-if="scope.row.orderStatus==2">已发货</span>
        <span v-if="scope.row.orderStatus==3">完成</span>
        <span v-if="scope.row.orderStatus==-1">取消</span>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180px">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="支付时间" align="center" prop="paymentTime" width="180">-->
<!--        <template #default="scope">-->
<!--          <span>{{ parseTime(scope.row.paymentTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="收货人姓名" align="center" prop="receiverName" />
      <el-table-column label="收货人电话" align="center" prop="receiverPhone" />
      <el-table-column label="收货人详细地址" align="center" prop="receiverAddress" width="300px"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="getDetail(scope.row)"
                     v-hasPermi="['order:orderInfo:edit']">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 详情 -->
    <el-dialog :title="title" v-model="open" width="1100px" append-to-body>
      <el-form ref="orderInfoRef" :model="form"  label-width="80px">
        <hr>
        <el-form-item label="基本信息" >
          <div style="width: 500px;">
            <el-form-item label="订单号" prop="orderNo">
              {{form.orderNo}}
            </el-form-item>
            <el-form-item label="优惠券" prop="couponAmount">
              {{form.couponAmount}}
            </el-form-item>
            <el-form-item label="运费" prop="feightFee">
              {{form.feightFee}}
            </el-form-item>
            <el-form-item label="订单状态" prop="feightFee">
              <span v-if="form.orderStatus==0">待支付</span>
              <span v-if="form.orderStatus==1">待发货</span>
              <span v-if="form.orderStatus==2">已发货</span>
              <span v-if="form.orderStatus==3">完成</span>
              <span v-if="form.orderStatus==-1">取消</span>
            </el-form-item>
            <el-form-item label="支付时间" prop="feightFee">
              <span>{{ parseTime(paymentTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
            </el-form-item>

          </div>
          <div>
            <el-form-item label="订单总额" prop="totalAmount">
              {{form.totalAmount}}
            </el-form-item>
            <el-form-item label="原价金额" prop="originalTotalAmount">
              {{form.originalTotalAmount}}
            </el-form-item>
            <el-form-item label="支付方式" prop="originalTotalAmount">
              {{form.paymentType}}
            </el-form-item>
            <el-form-item label="创建时间" prop="createTime">
              <span>{{ parseTime(createTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
            </el-form-item>
            <el-form-item label="订单备注" prop="remark">
              {{form.remark}}
            </el-form-item>
          </div>
        </el-form-item>
        <hr>
        <el-form-item label="收货人信息" label-width="85px" :inline-message="true">
          <div style="width: 500px;">
            <el-form-item label="收货人姓名" prop="receiverName">
              {{form.receiverName}}
            </el-form-item>
            <el-form-item label="地址标签" prop="receiverTagName">
              {{form.receiverTagName}}
            </el-form-item>

          </div>
          <div>
            <el-form-item label="收货人电话" prop="receiverPhone">
              {{form.receiverPhone}}
            </el-form-item>
            <el-form-item label="详细地址" prop="receiverAddress">
              {{form.receiverAddress}}
            </el-form-item>
          </div>
        </el-form-item>
        <hr>

        <el-form-item label="订单明细信息" label-width="100px">
          <el-table :data="skuList" style="width: 100%">
            <el-table-column prop="skuName" label="SKU名称" width="600" />
            <el-table-column  label="图片" width="120" #default="scope">
              <img :src="scope.row.thumbImg" width="50">
            </el-table-column>
            <el-table-column prop="skuPrice" label="SKU价格" width="120"/>
            <el-table-column prop="skuNum" label="购买数量" width="120"/>
          </el-table>
        </el-form-item>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="OrderInfo">
import { listOrderInfo, getOrderInfo, delOrderInfo, addOrderInfo, updateOrderInfo } from "@/api/order/orderInfo";
import {parseTime} from "../../../utils/ruoyi.js";
import {getSkuByOrderId} from "../../../api/order/orderItem.js";

const { proxy } = getCurrentInstance();

const orderInfoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const skuList = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userId: null,
    nickName: null,
    orderNo: null,
    couponId: null,
    totalAmount: null,
    couponAmount: null,
    originalTotalAmount: null,
    feightFee: null,
    orderStatus: null,
    receiverName: null,
    receiverPhone: null,
    receiverTagName: null,
    receiverProvince: null,
    receiverCity: null,
    receiverDistrict: null,
    receiverAddress: null,
    paymentTime: null,
    deliveryTime: null,
    receiveTime: null,
    cancelTime: null,
    cancelReason: null,
    startTime: null,
    endTime: null,
  }
});

const { queryParams, form, rules } = toRefs(data);
const betweenTime = ref([])

function chooseDate(){
  queryParams.value.startTime = parseTime(betweenTime.value[0], '{y}-{m}-{d} {h}:{m}:{s}')
  queryParams.value.endTime = parseTime(betweenTime.value[1], '{y}-{m}-{d} {h}:{m}:{s}')
  handleQuery()
}
/** 查询订单列表 */
function getList() {
  loading.value = true;
  listOrderInfo(queryParams.value).then(response => {
    orderInfoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    userId: null,
    nickName: null,
    orderNo: null,
    couponId: null,
    totalAmount: null,
    couponAmount: null,
    originalTotalAmount: null,
    feightFee: null,
    orderStatus: null,
    receiverName: null,
    receiverPhone: null,
    receiverTagName: null,
    receiverProvince: null,
    receiverCity: null,
    receiverDistrict: null,
    receiverAddress: null,
    paymentTime: null,
    deliveryTime: null,
    receiveTime: null,
    cancelTime: null,
    cancelReason: null,
    createTime: null,
    createBy: null,
    updateTime: null,
    updateBy: null,
    delFlag: null,
    remark: null,
    startTime: null,
    endTime: null,
    paymentType: null,

};
  proxy.resetForm("orderInfoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}


/** 修改按钮操作 */
function getDetail(row) {
  reset();
  const _id = row.id || ids.value
  getOrderInfo(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "订单详情";
  });
  getSkuByOrderId(_id).then(response => {
    skuList.value = response.data
    console.log(response)
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["orderInfoRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateOrderInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addOrderInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}


/** 导出按钮操作 */
function handleExport() {
  proxy.download('order/orderInfo/export', {
    ...queryParams.value
  }, `orderInfo_${new Date().getTime()}.xlsx`)
}

getList();
</script>
